package com.gjzt.business.media.service;

import java.util.List;

import net.sf.json.JSONArray;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eastdigit.servlet.ReqBean;
import com.eastdigit.system.model.BusinessException;
import com.eastdigit.system.model.SqlBuilder;
import com.eastdigit.system.service.BaseService;
import com.eastdigit.util.JSONUtil;
import com.eastdigit.util.StringUtil;
import com.gjzt.business.media.inter.MediaTypeConfigMapper;
import com.gjzt.business.media.inter.MediaTypeMapper;
import com.gjzt.business.media.model.MediaType;
import com.gjzt.business.media.model.MediaTypeConfig;
import com.gjzt.business.utils.Constant;

@Service
public class MediaTypeService extends BaseService {

    @Autowired
    private MediaTypeMapper mediaTypeMapper;
    
    @Autowired
    private MediaTypeConfigMapper mediaTypeConfigMapper;

    /**
     * 查询
     *
     * @param reqBean
     * @return
     */
    public JSONArray findMediaType(ReqBean reqBean) {

        String mediaType = reqBean.getInfo("mediaType");
        if(StringUtils.isBlank(mediaType)) {
            throw new BusinessException("缺少参数!");
        }
        SqlBuilder sqlBuilder = new SqlBuilder("select * from MEDIA_TYPE a where 1=1 ").addWhere("a.PARENT_ID", mediaType)
                .addWhereLike2("a.MEDIA_NAME", reqBean.getInfo("quickSearchName")).orderBy(" order by a.MEDIA_ORDER ");
        return this.query(sqlBuilder);
    }

    /**
     * 新增
     *
     * @param reqBean
     */
    public void addMediaType(ReqBean reqBean) {
        MediaType mediaType = getMediaType(reqBean);
        mediaType.setMediaId(intIncrementId("MEDIA_TYPE","MEDIA_ID"));
        mediaType.setStatus(Constant.DATA_STATUS_TRUE);
        mediaType.setEditable(MediaType.EDITABLE_TRUE);
        mediaType.setMediaOrder(mediaTypeMapper.getMaxMediaOrder(mediaType.getParentId()) + 1);
        mediaTypeMapper.insert(mediaType);
    }
    
    public MediaType getMediaType(String mediaId){
    	return mediaTypeMapper.selectByPrimaryKey(mediaId);
    }

    /**
     * 修改
     *
     * @param reqBean
     */
    public void updateMediaType(ReqBean reqBean) {
        String mediaId = reqBean.getInfo("mediaId");
        if (StringUtil.isEmpty(mediaId)) {
            throw new BusinessException("获取主键失败");
        }
        MediaType mediaType = getMediaType(reqBean);
        mediaTypeMapper.updateByPrimaryKeySelective(mediaType);
    }

    /**
     * 从表单获取对象
     *
     * @param reqBean
     * @return
     */
    private MediaType getMediaType(ReqBean reqBean) {
        MediaType mediaType = new MediaType();

        mediaType.setMediaId(reqBean.getInfo("mediaId"));
        mediaType.setParentId(reqBean.getInfo("parentId"));
        mediaType.setMediaName(reqBean.getInfo("mediaName"));
        mediaType.setStatus(reqBean.getInfo("status"));
        mediaType.setEditable(reqBean.getInfo("editable"));
        mediaType.setMediaOrder(0);
        mediaType.setModelType(reqBean.getInfo("modelType"));
        return mediaType;
    }

    /**
     * 查询顶层类型
     * @param reqBean
     * @return
     */
    public JSONArray queryTopType(ReqBean reqBean) {
        String sql = "SELECT a.* FROM `MEDIA_TYPE` a where a.PARENT_ID = '0'";
        return query(sql);
    }
    
    
    public void disableMediaType(String mediaId){
    	if (StringUtil.isEmpty(mediaId)) {
            throw new BusinessException("获取主键失败");
        }
    	mediaTypeMapper.updateStatusByPrimaryKey(mediaId, Constant.DATA_STATUS_FALSE);
    }
    
    public void revertMediaType(String mediaId){
    	if (StringUtil.isEmpty(mediaId)) {
            throw new BusinessException("获取主键失败");
        }
    	mediaTypeMapper.updateStatusByPrimaryKey(mediaId, Constant.DATA_STATUS_TRUE);
    }
    
    /**
     * 获取有效车辆媒体分类
     * @return
     */
    public JSONArray queryBusMediaType() {
        String sql = "SELECT * FROM MEDIA_TYPE a where a.PARENT_ID = '1' and a.STATUS = '" + Constant.DATA_STATUS_TRUE + "' order by a.MEDIA_ORDER";
        return query(sql);
    }
    
    /**
     * 获取有效站点媒体分类
     * @return
     */
    public JSONArray queryStopMediaType() {
        String sql = "SELECT a.* FROM MEDIA_TYPE a where a.PARENT_ID = '2' and a.STATUS = '" + Constant.DATA_STATUS_TRUE + "' order by a.MEDIA_ORDER";
        return query(sql);
    }

    /**
     * 根据父类型查询下面的子类型
     * @param reqBean
     * @return
     */
	public JSONArray getMediaTypesByParentId(ReqBean reqBean) {
		String parentId = reqBean.getInfo("parentId");
		String sql = "select mt.MODEL_TYPE,mt.MEDIA_ID, mt.MEDIA_ID as value,mt.MEDIA_NAME as name  from  MEDIA_TYPE  mt  "
				+ " where mt.PARENT_ID=" + parentId+" and mt.STATUS = '" + Constant.DATA_STATUS_TRUE + "' order by mt.MEDIA_ORDER";
		return query(sql);
	}

    /**
     * 查询所有的分类
     * @param reqBean
     * @return
     */
    public JSONArray getAllMediaTypes(ReqBean reqBean) {
        String sql = "select a.*,a.MEDIA_ID value,a.MEDIA_NAME name from MEDIA_TYPE a where a.PARENT_ID <> '0' order by a.MEDIA_ORDER";
        return query(sql);
    }
    
    /**
     * 查询媒体类型配置
     * @param mediaId
     * @return
     */
    public JSONArray getItemsOfMedia(String mediaId){
    	List<MediaTypeConfig> mediaTypeConfigs = mediaTypeConfigMapper.selectByMediaId(mediaId);
    	return JSONArray.fromObject(mediaTypeConfigs,JSONUtil.getJsonConfig());
    }
}
